/*******************************************************************************
 * Copyright (c) 2011 Grupo de Sistemas Inteligentes (GSI) - DIT UPM
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *******************************************************************************/
package es.upm.dit.gsi.eclipse.jadex.adfmanager.wizards;

import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Button;

/**
 * This page allows to get the following information in order to create the new goal:
 *  - Goal name
 *  - Goal type
 *  - BDI Flags
 *  
 *  @author Pablo Muñoz
 */

public class AddGoalWizardPage1 extends WizardPage {
	private Text nameText;
	
	private Combo typeCombo;
	
	private Combo retryCombo;
	
	private Button enableRetry;
	
	private Text retryDelayText;
	
	private Button enableRetryDelayText;
	
	private Combo excludeCombo;
	
	private Button enableExcludeCombo;
	
	private Combo posttoallCombo;
	
	private Button enablePosttoallCombo;
	
	private Combo randomSelectionCombo;
	
	private Button enableRandomSelectionCombo;
	
	private Combo metalevelreasoningCombo;
	
	private Button enableMetalevelreasoningCombo;
	
	private Combo recurCombo;
	
	private Button enableRecurCombo;
	
	private Text recurDelayText;
	
	private Button enableRecurDelayText;
	

	/**
	 * Constructor for AddGoalWizardPage1
	 * 
	 * @param pageName
	 */
	public AddGoalWizardPage1(ISelection selection) {
		super("wizardPage");
		setTitle("Add Goal Wizard");
		setDescription("Page 1: Goal details");
	}

	/**
	 * @see IDialogPage#createControl(Composite)
	 */
	public void createControl(Composite parent) {
		Composite container = new Composite(parent, SWT.NULL);
		GridLayout layout = new GridLayout();
		container.setLayout(layout);
		layout.numColumns = 3;
		layout.horizontalSpacing = 50;
		layout.verticalSpacing = 9;
		
		Label label1 = new Label(container, SWT.NULL);
		label1.setText("&Name:");
		nameText = new Text(container, SWT.BORDER | SWT.SINGLE);
		GridData gd = new GridData(GridData.FILL_HORIZONTAL);
		nameText.setLayoutData(gd);
		gd.horizontalSpan = 2;
		nameText.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				dialogChanged();
			}
		});
		
		Label label2 = new Label(container, SWT.NULL);
		label2.setText("&Type:");
		typeCombo = new Combo(container, SWT.READ_ONLY);
		typeCombo.setItems (new String [] {"performgoal","achievegoal", "querygoal", "maintaingoal","metagoal"});
		typeCombo.select(0);
		gd = new GridData(GridData.FILL_HORIZONTAL);
		gd.horizontalSpan = 2;
		typeCombo.setLayoutData(gd);
		typeCombo.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				dialogChanged();
			}
		});
		
		Label separator = new Label(container, SWT.SEPARATOR | SWT.HORIZONTAL);
		gd = new GridData(GridData.FILL_HORIZONTAL);
		separator.setLayoutData(gd);
		gd.horizontalSpan = 3;		
		
		Label label3 = new Label(container, SWT.NULL);
		label3.setText("&Retry (optional):");
		retryCombo = new Combo(container, SWT.READ_ONLY);
		retryCombo.setItems (new String [] {"true","false"});
		retryCombo.select(0);
		gd = new GridData(GridData.FILL_HORIZONTAL);
		retryCombo.setLayoutData(gd);
		retryCombo.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				dialogChanged();
			}
		});
		enableRetry = new Button (container, SWT.CHECK);
		enableRetry.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				enabledPropertiesChanged();
			}
		});
		
		Label label4 = new Label(container, SWT.NULL);
		label4.setText("&Retry delay (optional):");
		retryDelayText = new Text(container, SWT.BORDER | SWT.SINGLE);
		gd = new GridData(GridData.FILL_HORIZONTAL);
		retryDelayText.setLayoutData(gd);
		retryDelayText.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				dialogChanged();
			}
		});
		enableRetryDelayText = new Button (container, SWT.CHECK);
		enableRetryDelayText.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				enabledPropertiesChanged();
			}
		});
		
		Label label5 = new Label(container, SWT.NULL);
		label5.setText("&Exclude (optional):");
		excludeCombo = new Combo(container, SWT.READ_ONLY);
		excludeCombo.setItems (new String [] {"when_tried","when_failed","when_succeeded","never"});
		excludeCombo.select(0);
		gd = new GridData(GridData.FILL_HORIZONTAL);
		excludeCombo.setLayoutData(gd);
		excludeCombo.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				dialogChanged();
			}
		});
		enableExcludeCombo = new Button (container, SWT.CHECK);
		enableExcludeCombo.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				enabledPropertiesChanged();
			}
		});
		
		Label label6 = new Label(container, SWT.NULL);
		label6.setText("&Posttoall (optional):");
		posttoallCombo = new Combo(container, SWT.READ_ONLY);
		posttoallCombo.setItems (new String [] {"true","false"});
		posttoallCombo.select(0);
		gd = new GridData(GridData.FILL_HORIZONTAL);
		posttoallCombo.setLayoutData(gd);
		posttoallCombo.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				dialogChanged();
			}
		});
		enablePosttoallCombo= new Button (container, SWT.CHECK);
		enablePosttoallCombo.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				enabledPropertiesChanged();
			}
		});
		
		Label label7 = new Label(container, SWT.NULL);
		label7.setText("&Randomselection (optional):");
		randomSelectionCombo = new Combo(container, SWT.READ_ONLY);
		randomSelectionCombo.setItems (new String [] {"true","false"});
		randomSelectionCombo.select(0);
		gd = new GridData(GridData.FILL_HORIZONTAL);
		randomSelectionCombo.setLayoutData(gd);
		randomSelectionCombo.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				dialogChanged();
			}
		});
		enableRandomSelectionCombo= new Button (container, SWT.CHECK);
		enableRandomSelectionCombo.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				enabledPropertiesChanged();
			}
		});
		
		Label label8 = new Label(container, SWT.NULL);
		label8.setText("&Metalevelreasoning (optional):");
		metalevelreasoningCombo = new Combo(container, SWT.READ_ONLY);
		metalevelreasoningCombo.setItems (new String [] {"true","false"});
		metalevelreasoningCombo.select(0);
		gd = new GridData(GridData.FILL_HORIZONTAL);
		metalevelreasoningCombo.setLayoutData(gd);
		metalevelreasoningCombo.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				dialogChanged();
			}
		});
		enableMetalevelreasoningCombo= new Button (container, SWT.CHECK);
		enableMetalevelreasoningCombo.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				enabledPropertiesChanged();
			}
		});
		
		Label label9 = new Label(container, SWT.NULL);
		label9.setText("&Recur (optional):");
		recurCombo = new Combo(container, SWT.READ_ONLY);
		recurCombo.setItems (new String [] {"true","false"});
		recurCombo.select(0);
		gd = new GridData(GridData.FILL_HORIZONTAL);
		recurCombo.setLayoutData(gd);
		recurCombo.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				dialogChanged();
			}
		});
		enableRecurCombo= new Button (container, SWT.CHECK);
		enableRecurCombo.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				enabledPropertiesChanged();
			}
		});
		
		Label label10 = new Label(container, SWT.NULL);
		label10.setText("&Recur delay (optional):");
		recurDelayText = new Text(container, SWT.BORDER | SWT.SINGLE);
		gd = new GridData(GridData.FILL_HORIZONTAL);
		recurDelayText.setLayoutData(gd);
		recurDelayText.setLayoutData(gd);
		recurDelayText.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				dialogChanged();
			}
		});
		enableRecurDelayText = new Button (container, SWT.CHECK);
		enableRecurDelayText.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				enabledPropertiesChanged();
			}
		});	
		
		initialize();
		dialogChanged();
		setControl(container);
	}


	private void initialize() {
		enabledPropertiesChanged();
	}

	/*
	 * Ensures that all text fields are set.
	 */
	private void dialogChanged() {
		if(nameText.getText().length() == 0){
			updateStatus("A name must be specified for the new Plan");
			return;
		}
		if(retryDelayText.isEnabled() && retryDelayText.getText().length() == 0){
			updateStatus("A retry delay must be specified");
			return;
		}
		if(recurDelayText.isEnabled() && recurDelayText.getText().length() ==0){
			updateStatus("Recur Delay must be specified");
			return;
		}
		if(retryDelayText.isEnabled() && !checkNumberFormat(retryDelayText.getText())){
			updateStatus("Retry delay must be an integer");
			return;
		}
		if(recurDelayText.isEnabled() && !checkNumberFormat(recurDelayText.getText())){
			updateStatus("Recur delay must be an integer");
			return;
		}
		updateStatus(null);
	}
	
	/*
	 * This method is called when any 'enable' checkbox is checked
	 */
	private void enabledPropertiesChanged(){
		retryCombo.setEnabled(enableRetry.getSelection());
		retryDelayText.setEnabled(enableRetryDelayText.getSelection());
		excludeCombo.setEnabled(enableExcludeCombo.getSelection());	
		posttoallCombo.setEnabled(enablePosttoallCombo.getSelection());
		randomSelectionCombo.setEnabled(enableRandomSelectionCombo.getSelection());
		metalevelreasoningCombo.setEnabled(enableMetalevelreasoningCombo.getSelection());
		recurCombo.setEnabled(enableRecurCombo.getSelection());
		recurDelayText.setEnabled(enableRecurDelayText.getSelection());
		dialogChanged();
		
	}

	/*
	 * This method is called in order to update the status message 
	 */
	private void updateStatus(String message) {
		setErrorMessage(message);
		setPageComplete(message == null);
	}
	
	/*
	 * This method is called in order to check if the string s is a correct
	 * formatted Integer.
	 */
	private boolean checkNumberFormat(String s){
		try{
			Integer.decode(s);
			return true;
		}
		catch(NumberFormatException e1){
			return false;
		}
		catch(Exception e2){
			return false;
		}
	}

	/**
	 * @return the goal name
	 */
	public String getGoalName() {
		return nameText.getText();
	}

	/**
	 * @return the goal type
	 */
	public String getGoalType() {
		return typeCombo.getText();
	}

	/**
	 * @return the retry value or null if the control is disabled
	 */
	public String getRetry() {
		if(enableRetry.getSelection()){
			return retryCombo.getText();
		}
		return null;
	}

	/**
	 * @return the retry delay or null if the control is disabled
	 */
	public String getRetryDelay() {
		if(enableRetryDelayText.getSelection()){
			return retryDelayText.getText();
		}
		return null;
	}

	/**
	 * @return the exclude value or null if the control is disabled
	 */
	public String getExclude() {
		if(enableExcludeCombo.getSelection()){
			return excludeCombo.getText();
		}
		return null;
	}

	/**
	 * @return the 'post to all' value or null if the control is disabled
	 */
	public String getPosttoall() {
		if(enablePosttoallCombo.getSelection()){
			return posttoallCombo.getText();
		}
		return null;
	}

	/**
	 * @return the 'random selection' value or null if the control is disabled
	 */
	public String getRandomSelection() {
		if(enableRandomSelectionCombo.getSelection()){
			return randomSelectionCombo.getText();
		}
		return null;
	}
	
	/**
	 * @return the metalevelreasoning value or null if the control is disabled
	 */
	public String getMetalevelreasoning() {
		if(enableMetalevelreasoningCombo.getSelection()){
			return metalevelreasoningCombo.getText();
		}
		return null;
	}

	/**
	 * @return the recur value or null if the control is disabled
	 */
	public String getRecur() {
		if(enableRecurCombo.getSelection()){
			return recurCombo.getText();
		}
		return null;
	}

	/**
	 * @return the recur delay value or null if the control is disabled
	 */
	public String getRecurDelay() {
		if(enableRecurDelayText.getSelection()){
			return recurDelayText.getText();
		}
		return null;
	}
}